<?php
/**
 * 更新品牌荣耀（品牌榜）榜单的连续上榜次数
 * 更新时间：每周三，在榜单张榜之后。每周三0点3分
 */
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\WikiRankingCateHotUp;
use Biz\model\wiki\WikiRankingBrandData;
use Biz\model\wiki\WikiRankingCatePublic;
use Biz\model\wiki\WikiRankingBrand;
use Biz\model\wiki\WikiRankingWeek;
use Biz\service\wikiRanking\Ranking;

const HOT_UP_ID = 10000;
const PUBLIC_ID = 10001;

$start = time();
$dayArr = Ranking::getCreateRankingDay();
$day = $dayArr['day'];
$now = date('Ymd');
if ($day != $now) {
    var_dump('当前日期为：' . $now . '不能更新第「' . $day . '」的品牌荣耀榜单');
    return false;
}
$info = WikiRankingWeek::getByWhere(['ranking_day' => $day]);
if (!$info) {
    var_dump('不存在的榜单期数');
    return false;
}

//重置品牌 榜单的连续上榜次数
$res = WikiRankingBrandData::updateData(['continue_week' => 0], ['id[>]' => 0,'data_type' => [WikiRankingBrandData::TYPE_HOT_UP ,WikiRankingBrandData::TYPE_CATE_PUBLIC ]]);


$allWeekDay = WikiRankingWeek::selectByWhere([
    'ORDER' => ['ranking_day' => 'DESC'],
    'ranking_day[<=]' => $day,
    'ranking_day[>]' => 20211201,
], 'ranking_day');
//brand_info
$allBrandIds = WikiRankingBrand::selectByWhere(['ranking_state' => WikiRankingBrand::RANKING_STATE_SHOW], 'brand_id');

$allBrandIds = array_unique($allBrandIds);
$hotWhere = ['ranking_state' => WikiRankingCateHotUp::RANKING_STATE_SHOW, 'brand_id[>]' => 0];
$publicPraiseWhere = ['ranking_state' => WikiRankingCatePublic::RANKING_STATE_SHOW, 'brand_id[>]' => 0];

foreach ($allWeekDay as $index => $day) {

    $hotWhere['ranking_day'] = $day;
    $hotWhere['brand_id'] = $allBrandIds;

    $publicPraiseWhere['ranking_day'] = $day;
    $publicPraiseWhere['brand_id'] = $allBrandIds;

    $brandWhere['ranking_day'] = $day;
    //热度
    if ($hotWhere) {
        $hotRankingArr = WikiRankingCateHotUp::getDistinctBrand($hotWhere);
        $brandIds = array_column($hotRankingArr, 'brand_id');
        $allBrandIds = array_merge($allBrandIds, $brandIds);

        foreach ($hotRankingArr as $row) {
            $key = $row['brand_id'] . '_' . HOT_UP_ID;
            $allRankingBrandData[WikiRankingBrandData::TYPE_HOT_UP][$key] = $index + 1;
        }
    }
    //口碑
    if ($publicPraiseWhere) {
        $publicPraiseRankingArr = WikiRankingCatePublic::getDistinctBrand($publicPraiseWhere);
        $brandIds = array_column($publicPraiseRankingArr, 'brand_id');
        $allBrandIds = array_merge($allBrandIds, $brandIds);
        $allBrandIds = array_unique($allBrandIds);
        $publicPraiseWhere['ranking_id'] = [];
        foreach ($publicPraiseRankingArr as $row) {
            $key = $row['brand_id'] . '_' . PUBLIC_ID;
            $allRankingBrandData[WikiRankingBrandData::TYPE_CATE_PUBLIC][$key] = $index + 1;
        }
    }
}

foreach ($allRankingBrandData as $type => $rows) {
    if ($rows) {
        foreach ($rows as $key => $continueWeek) {
            $arr = explode('_', $key);
            $brandId = $arr[0];
            $rankingId = $arr[1];
            addData($brandId, $rankingId, $continueWeek, $type);
        }
    }
}

$end = time();
var_dump(date('Y-m-d-H-i-s') . '第' . $day . '期榜单,更新品牌荣耀（品牌榜）榜单的连续上榜次数,耗时:' . ($end - $start) . '秒，' . (($end - $start) / 60) . '分');

function addData($brandId, $rankingId, $continueWeek, $type)
{
    $data = [
        'brand_id' => $brandId,
        'ranking_id' => $rankingId,
        'data_type' => $type,
        'continue_week' => $continueWeek,
    ];
    $info = WikiRankingBrandData::getByWhere(['brand_id' => $brandId, 'ranking_id' => $rankingId, 'data_type' => $type]);;
    if ($info) {
        WikiRankingBrandData::updateData($data, ['id' => $info['id']]);
    } else {
        WikiRankingBrandData::insertData($data);
    }
}